package io.kiki.sba.registry.server.metadata.resource;

import io.kiki.sba.registry.common.model.console.PersistenceData;
import io.kiki.sba.registry.common.model.console.PersistenceDataBuilder;
import io.kiki.sba.registry.common.model.constants.ValueConstants;
import io.kiki.sba.registry.common.model.metaserver.ProvideDataChangeEvent;
import io.kiki.sba.registry.core.model.Result;
import io.kiki.sba.registry.server.metadata.provide.data.DefaultProvideDataNotifier;
import io.kiki.sba.registry.server.metadata.provide.data.ProvideDataService;
import io.kiki.sba.registry.server.metadata.resource.filter.LeaderAwareRestController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;


@Path("blacklist")
@LeaderAwareRestController
public class BlacklistDataResource {

    private static final Logger logger = LoggerFactory.getLogger(BlacklistDataResource.class);


    @Autowired
    private ProvideDataService provideDataService;

    @Autowired
    private DefaultProvideDataNotifier provideDataNotifier;

    /**
     * update blacklist e.g. curl -d
     * '{"FORBIDDEN_PUB":{"IP_FULL":["1.1.1.1","10.15.233.150"]},"FORBIDDEN_SUB_BY_PREFIX":{"IP_FULL":["1.1.1.1"]}}'
     * -H "Content-Type: application/json" -X POST http://localhost:9615/blacklist/update
     *
     * @param config config
     * @return Result
     */
    @POST
    @Path("update")
    @Produces(MediaType.APPLICATION_JSON)
    public Result blacklistPush(String config) {
        PersistenceData persistenceData = PersistenceDataBuilder.createPersistenceData(ValueConstants.BLACK_LIST_DATA_ID, config);
        try {

            boolean ret = provideDataService.saveProvideData(persistenceData);
            logger.info("Success update blacklist to DB result {}!", ret);
        } catch (Throwable e) {
            logger.error("Error update blacklist to DB!", e);
            throw new RuntimeException("Update blacklist to error!", e);
        }

        fireDataChangeNotify(persistenceData.getVersion(), ValueConstants.BLACK_LIST_DATA_ID);

        Result result = new Result();
        result.setSuccess(true);
        return result;
    }

    private void fireDataChangeNotify(Long version, String dataInfoId) {

        ProvideDataChangeEvent provideDataChangeEvent = new ProvideDataChangeEvent(dataInfoId, version);
        if (logger.isInfoEnabled()) {
            logger.info("send PERSISTENCE_DATA_CHANGE_NOTIFY_TASK notifyProvideDataChange: {}", provideDataChangeEvent);
        }
        provideDataNotifier.notifyProvideDataChange(provideDataChangeEvent);
    }


    public BlacklistDataResource setProvideDataService(ProvideDataService provideDataService) {
        this.provideDataService = provideDataService;
        return this;
    }


    public BlacklistDataResource setProvideDataNotifier(DefaultProvideDataNotifier provideDataNotifier) {
        this.provideDataNotifier = provideDataNotifier;
        return this;
    }
}
